package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.utils.TimeUtils;
import cn.android.support.v7.lib.sin.crown.view.WheelTurns;

/**
 * Created by 彭治铭 on 2017/9/22.
 */

public class DateCompleteDialog extends BaseDialog {
    private static DateCompleteDialog dateDialog;

    private String yyyy, MM, dd, HH, mm;//默认选中日期

//                调用案例
//                DateCompleteDialog.getInstance(MainActivity.this, "2018", "1", "2","7","50", new BaseListenner<List<String>>() {
//                    @Override
//                    public void onResult(List<String> result) {
//                        Log.e("test","选中:\t年：\t"+result.get(0)+"\t月:\t"+result.get(1)+"\t日：\t"+result.get(2)+"\t时:\t"+result.get(3)+"\t分:\t"+result.get(4));
//                    }
//                });

    //Integer.valueOf("009")会自动转换成9
    //DateDialog.getInstance(this,Integer.valueOf(UtilTime.getInstance().getAbsoluteTime("yyyy")),Integer.valueOf(UtilTime.getInstance().getAbsoluteTime("MM")),Integer.valueOf(UtilTime.getInstance().getAbsoluteTime("dd")));
    //回調返回list數組，年，月，日,时，分
    public static DateCompleteDialog getInstance(Activity activity, String yyyy, String MM, String dd, String HH, String mm, BaseListenner<List<String>> baseListenner) {
        if (isfinish(activity,dateDialog)) {
            dateDialog = new DateCompleteDialog(activity);
        }
        dateDialog.yyyy = yyyy;
        dateDialog.MM = MM;
        dateDialog.dd = dd;
        dateDialog.HH = HH;
        dateDialog.mm = mm;
        dateDialog.baseListenner = baseListenner;
        return dateDialog;
    }

    public DateCompleteDialog(Activity activity) {
        init(activity, R.layout.crown_dialog_date_complete);
        window.setWindowAnimations(R.style.crown_window_bottom);
        initEx();
        initData();
    }

    BaseListenner<List<String>> baseListenner;

    @Override
    protected void initUI() {
        buttonComplete = (Button) findViewById(R.id.crown_btn_complete);
        year = (WheelTurns) findViewById(R.id.crown_wheelYear);
        month = (WheelTurns) findViewById(R.id.crown_wheelMonth);
        day = (WheelTurns) findViewById(R.id.crown_wheelDay);
        hour = (WheelTurns) findViewById(R.id.crown_wheelHour);
        minutes = (WheelTurns) findViewById(R.id.crown_wheelMinutes);
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(activity, findViewById(R.id.crown_date_dialog));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_startLine));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_endLine));
        ProportionUtils.getInstance().adapterButton(buttonComplete);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return false;
    }

    @Override
    protected void listener() {

        int mDay = TimeUtils.getInstance().getMonthDay(this.yyyy + "-" + MM);
        if (day.getSize() != mDay) {
            List<String> days = new ArrayList<>();
            for (int i = 1; i <= mDay; i++) {
                days.add(i + "");
            }
            day.setData(days, "日", width, height, count);//刚刚设置数据，如果马上选择指定日期可能无效。
        }

        findViewById(R.id.crown_date_dialog_bg).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    dismiss();
                }
                return true;
            }
        });

        buttonComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (baseListenner != null) {
                    List<String> list = new ArrayList<String>();
                    list.add(year.curentValue);//年
                    list.add(month.curentValue);//月
                    list.add(day.curentValue);//日
                    list.add(hour.curentValue);//时
                    list.add(minutes.curentValue);//分
                    baseListenner.onResult(list);
                }
                dismiss();
            }
        });


        year.postDelayed(new Runnable() {
            @Override
            public void run() {
                //选中默认值
                year.selectVaules(yyyy + "", false);
                month.selectVaules(MM + "", false);
                day.selectVaules(dd + "", false);
                hour.selectVaules(HH + "", false);
                minutes.selectVaules(mm + "", false);
            }
        }, 50);

    }

    @Override
    protected void recycleView() {

    }

    Button buttonComplete;
    WheelTurns year, month, day;
    WheelTurns hour, minutes;

    public Button crown_btn_cancel, crown_btn_info;

    //设置右边完成按钮信息
    public DateCompleteDialog setBtnCompleteText(String info) {
        buttonComplete.setText(info);
        return this;
    }

    //设置右边完成按钮字体颜色
    public DateCompleteDialog setBtnCompleteColor(int color) {
        buttonComplete.setTextColor(color);
        return this;
    }

    //设置中间按钮信息
    public DateCompleteDialog setBtnInfoText(String info) {
        crown_btn_info.setText(info);
        return this;
    }

    //设置中间按钮字体颜色
    public DateCompleteDialog setBtnInfoColor(int color) {
        crown_btn_info.setTextColor(color);
        return this;
    }


    //设置取消按钮信息
    public DateCompleteDialog setBtnCancelText(String info) {
        crown_btn_cancel.setText(info);
        if (info == null || info.trim().equals("")) {
            crown_btn_cancel.setOnClickListener(null);
        } else {
            crown_btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        return this;
    }

    //设置取消按钮字体颜色
    public DateCompleteDialog setBtnCancelColor(int color) {
        crown_btn_cancel.setTextColor(color);
        return this;
    }


    private void initEx() {
        crown_btn_info = (Button) findViewById(R.id.crown_btn_info);
        crown_btn_cancel = (Button) findViewById(R.id.crown_btn_cancel);
        ProportionUtils.getInstance().adapterButton(crown_btn_cancel);
        ProportionUtils.getInstance().adapterButton(crown_btn_info);
        crown_btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    int width, height, count;

    private void initData() {
        //年
        List<String> years = new ArrayList<>();
        int yyyy = Integer.valueOf(TimeUtils.getInstance().getAbsoluteTime("yyyy"));
        for (int i = 1897; i <= yyyy; i++) {
            years.add(i + "");
        }
        //月
        List<String> months = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            months.add(i + "");
        }
        width = findViewById(R.id.crown_date_dialog).getLayoutParams().width / 5;
        height = ProportionUtils.getInstance().adapterInt(360);
        count = 5;
        year.setData(years, "年", width, height, count);
        month.setData(months, "月", width, height, count);

        //日
        month.setBaseListenner(new BaseListenner<String>() {
            @Override
            public void onResult(String result) {
                //日期受月份影响，联动
                List<String> days = new ArrayList<>();
                final int mDay = TimeUtils.getInstance().getMonthDay(year.curentValue + "-" + result);//天数
                for (int i = 1; i <= mDay; i++) {
                    days.add(i + "");
                }
                int dayValue = Integer.valueOf(day.curentValue);//原有选中值
                day.upData(days, "日");
                if (mDay < dayValue) {
                    day.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //选中默认值
                            day.selectVaules(mDay + "", true);
                        }
                    }, 50);
                }

            }
        });

        //时
        List<String> hours = new ArrayList<>();
        for (int i = 1; i <= 24; i++) {
            hours.add(i + "");
        }
        hour.setData(hours, "时", width, height, count);

        //分
        List<String> minute = new ArrayList<>();
        for (int i = 0; i <= 59; i++) {
            minute.add(i + "");
        }
        minutes.setData(minute, "分", width, height, count);
    }

    public static void setDateDialog(DateCompleteDialog dateDialog) {
        DateCompleteDialog.dateDialog = dateDialog;
    }

    public void setYyyy(String yyyy) {
        this.yyyy = yyyy;
    }

    public void setMM(String MM) {
        this.MM = MM;
    }

    public void setDd(String dd) {
        this.dd = dd;
    }

    public void setHH(String HH) {
        this.HH = HH;
    }

    public void setMm(String mm) {
        this.mm = mm;
    }

    public void setBaseListenner(BaseListenner<List<String>> baseListenner) {
        this.baseListenner = baseListenner;
    }

    public void setButtonComplete(Button buttonComplete) {
        this.buttonComplete = buttonComplete;
    }

    public void setYear(WheelTurns year) {
        this.year = year;
    }

    public void setMonth(WheelTurns month) {
        this.month = month;
    }

    public void setDay(WheelTurns day) {
        this.day = day;
    }

    public void setHour(WheelTurns hour) {
        this.hour = hour;
    }

    public void setMinutes(WheelTurns minutes) {
        this.minutes = minutes;
    }

    public void setCrown_btn_cancel(Button crown_btn_cancel) {
        this.crown_btn_cancel = crown_btn_cancel;
    }

    public void setCrown_btn_info(Button crown_btn_info) {
        this.crown_btn_info = crown_btn_info;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
